import { createRouter, createWebHashHistory } from "vue-router";
import {getSessionStorage} from "../common.js";

const routes = [
    {
        path: "/",
        redirect:"/login",
    },
    {
        path:"/index",
        name:"index",
        component: ()=>import("../views/Index.vue"),
        meta: {
            keepAlive: false,
        }

    },{
        path: "/login",
        name:"login",
        component: ()=>import("../views/Login.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path: "/register",
        name:"register",
        component: ()=>import("../views/Register.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/businessList",
        name:"businessList",
        component: ()=>import("../views/BusinessList.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/order",
        name:"order",
        component: ()=>import("../views/Order.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/my",
        name:"my",
        component: ()=>import("../views/My.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/businessInfo/:businessId",
        name:"businessInfo",
        component: ()=>import("../views/BusinessInfo.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/confirmOrder",
        name:"confirmOrder",
        component: ()=>import("../views/confirmOrder.vue"),
        meta: {
            keepAlive: true,
        }
    },{
        path:"/payment/:orderId",
        name:"payment",
        component: ()=>import("../views/Payment.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/businessListByOrder/:orderType",
        name:"businessListOrderType",
        component: ()=>import("../views/BusinessList.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/userAddress",
        name:"userAddress",
        component: ()=>import("../views/UserAddress.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/addUserAddress",
        name:"addUserAddress",
        component: ()=>import("../views/AddUserAddress.vue"),
        meta: {
            keepAlive: false,
        }
    },{
        path:"/editUserAddress/:addressId",
        name:"editUserAddress",
        component: ()=>import("../views/EditUserAddress.vue"),
        meta: {
            keepAlive: false,
        }
    }
]



const router = createRouter({
    history: createWebHashHistory(),
    routes
});
router.beforeEach(function(to,from,next){
    let user = getSessionStorage("user");
    //除了登录、注册、首页、商家列表、商家信息之外，都需要判断是否登录
    if(!(to.path==='/'||to.path==='/index'||to.path==='/businessList'||to.path==='/businessInfo'||to.path==='/login'||to.path==='/register')){
            if(user==null){
                router.push('/login');
                location.reload();
            }
}
    next();
});
export default router;